View Javadoc
1 /*** 2 * Title: S/MIME Project 3 * Description: Creating S/MIME email transport capabilities. 4 * Copyright: Copyright (c) 2001 5 * @Author Vladimir Radisic 6 * @Version 2.0.1 7 */ 8 9 package org.webdocwf.util.smime.util; 10 11 12 import org.webdocwf.util.smime.exception.ErrorStorage; 13 import org.webdocwf.util.smime.exception.SMIMEException; 14 import java.net.InetAddress; 15 import java.util.Random; 16 import java.io.File; 17 import javax.mail.internet.MimeMessage; 18 import java.io.FileInputStream; 19 import java.io.ByteArrayOutputStream; 20 import javax.mail.Header; 21 import java.util.Enumeration; 22 import javax.activation.MimetypesFileTypeMap; 23 import java.io.*; 24 import org.bouncycastle.util.encoders.Base64; 25 26 27 /*** 28 * MimeAssist contains static methods which help in manipulation and work 29 * with mime messages. 30 */ 31 public class MimeAssist { 32 33 /*** 34 * Unique number for one session; 35 */ 36 private static long sesionIncrement = 0L; 37 38 /*** 39 * Converts email messages, previously composed in Java MimeMessage object, 40 * to byte array suitable for later processing within CMS object. Input 41 * (MimeMessage) has all the necessary data for sending MIME message 42 * (attachments, text content, Content-Type, Content-Transfer-Encoding etc). 43 * All RFC822 header lines except Content-Type, Content-Transfer-Encoding, 44 * Content-Disposition and Content-Description are removed in proces of 45 * converting. 46 * @param message0 MIME email prepared for sending as MimeMessage 47 * @return Byte array representation of MIME email prepared for later use in 48 * criptographic processing. 49 * @exception SMIMEException if there is no MimeBodyPart in MimeMultipart 50 * message, or in the cases of unrecognisable content of bodypart in MIME 51 * multipart/mixed message or unrecognisable Content-Transfer-Encoding of 52 * message. Also, it can be caused by non SMIMEException which can be one of 53 * the following: MessagingException, IOException or 54 * UnsupportedEncodingException. 55 */ 56 public static byte[] messageConvertor(MimeMessage message0) throws SMIMEException { 57 String s = new String(); // Container for message 58 byte[] returnByteArray = null; 59 60 try { 61 MimeMessage message = new MimeMessage(message0); 62 63 Enumeration enum = message0.getAllHeaders(); 64 65 while (enum.hasMoreElements()) { 66 Header temp = (Header) enum.nextElement(); 67 68 if (!(temp.getName().equalsIgnoreCase("Content-Type") || 69 temp.getName().equalsIgnoreCase("Content-Transfer-Encoding") || 70 temp.getName().equalsIgnoreCase("Content-Disposition") || 71 temp.getName().equalsIgnoreCase("Content-Description"))) { 72 message.removeHeader(temp.getName()); 73 } 74 } 75 76 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 77 String[] excludeHeaders = {"Message-ID", "Mime-Version"}; 78 79 message.writeTo(baos, excludeHeaders); 80 s = baos.toString(); 81 82 returnByteArray = s.getBytes("ISO-8859-1"); 83 } catch (Exception e) { 84 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 85 e, "MessageConvertor"); 86 } 87 return returnByteArray; 88 } 89 90 /*** 91 * Converts email messages, previously composed in Java MimeMessage object, 92 * to String suitable for later criptographic processing. Input 93 * (MimeMessage) has all the necessery data for sending MIME message 94 * (attachments, text content, Content-Type, Content-Transfer-Encoding etc). 95 * All RFC822 header lines except Content-Type, Content-Transfer-Encoding, 96 * Content-Disposition and Content-Description are removed in the process of 97 * converting. 98 * @param message0 MIME email prepared for sending as MimeMessage 99 * @return String representation of MIME email prepared for later use in 100 * criptographic processing. 101 * @exception SMIMEException if there is no MimeBodyPart in MimeMultipart 102 * message, or in the cases of unrecognisable content of bodypart in MIME 103 * multipart/mixed message or unrecognisable Content-Transfer-Encoding of 104 * message. Also, it can be caused by non SMIMEException which can be one of 105 * the following: MessagingException or IOException. 106 */ 107 public static String messageStringConvertor(MimeMessage message0) throws SMIMEException { 108 String s = new String(); // Container for message 109 110 try { 111 MimeMessage message = new MimeMessage(message0); 112 113 Enumeration enum = message0.getAllHeaders(); 114 115 while (enum.hasMoreElements()) { 116 Header temp = (Header) enum.nextElement(); 117 118 if (!(temp.getName().equalsIgnoreCase("Content-Type") || 119 temp.getName().equalsIgnoreCase("Content-Transfer-Encoding") || 120 temp.getName().equalsIgnoreCase("Content-Disposition") || 121 temp.getName().equalsIgnoreCase("Content-Description"))) { 122 message.removeHeader(temp.getName()); 123 } 124 } 125 126 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 127 String[] excludeHeaders = {"Message-ID", "Mime-Version"}; 128 129 message.writeTo(baos, excludeHeaders); 130 s = baos.toString(); 131 } catch (Exception e) { 132 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 133 e, "messageStringConvertor"); 134 } 135 return s; 136 } 137 138 /*** 139 * Generates value for Content-ID MIME header line which is used in construction 140 * of multipart/related type of MimeMessage. Generated value is composed from 141 * four parts in order to be unique: random number, unique session number, current 142 * date and time defined in milliseconds and the host name.<BR> 143 * <BR> 144 * <random-number>-<unique_session_number>-<date_&_time_in_ms>@<host-name> 145 * <BR> 146 * @return Content-ID value represented as String. 147 * @exception SMIMEException caused by non SMIMEException which is 148 * UnknownHostException 149 */ 150 public static String generateID() throws SMIMEException { 151 152 String contentID = ""; 153 String hostName = ""; 154 155 try { 156 hostName = InetAddress.getLocalHost().getHostName(); 157 } catch (Exception e) { 158 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 159 e, "generateID"); 160 } 161 162 Random random = new Random(System.currentTimeMillis()); 163 164 contentID = random.nextLong() + "$" + sesionIncrement + "$" + 165 System.currentTimeMillis() + "@" + hostName; 166 sesionIncrement++; 167 168 return contentID; 169 } 170 171 /*** 172 * Returns object of class MimetypesFileTypeMap generated by using data stored 173 * in the given file. For more information see Java documentation related to class 174 * MimetypesFileTypeMap. 175 * @param path0 path and file name of the file which contains desired mime-type 176 * definitions in appropriate format (usually file name is mime.types). 177 * @return MimetypesFileTypeMap object which can be used for getting appropriate 178 * mime-types from desired file names. 179 * @SMIMEException caused with non SMIMEException which is IOException 180 */ 181 public static MimetypesFileTypeMap getFileTypeMap(String path0) throws SMIMEException { 182 try { 183 return new MimetypesFileTypeMap(new FileInputStream(path0)); 184 } catch (Exception e) { 185 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 186 e, "getFileTypeMap"); 187 } 188 } 189 190 /*** 191 * Returns mime-type for given file name and extension. 192 * @param file0 is File type object that represents file which mime-type is 193 * looking for. 194 * @param mimeFile0 path and file name of the file which contains desired mime-type 195 * definitions in appropriate format (usually file name is mime.types). 196 * @return mime-type for given file. 197 * @SMIMEException caused with non SMIMEException which is IOException 198 */ 199 public static String getMimeTypeFromFileName(File file0, String mimeFile0) throws SMIMEException { 200 201 String contType; 202 203 try { 204 contType = getFileTypeMap(mimeFile0).getContentType(file0); 205 } catch (Exception e) { 206 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 207 e, "getMimeTypeFromFileName"); 208 } 209 return contType; 210 } 211 212 /*** 213 * Returns mime-type for given file name and extension. 214 * @param file0 path and file name of the file which mime-type is looking for. 215 * @param mimeFile0 path and file name to file which contains desired mime-type 216 * definitions in appropriate format (usually file name is mime.types). 217 * @return mime-type for given file. 218 * @SMIMEException caused with non SMIMEException which is IOException 219 */ 220 public static String getMimeTypeFromFileName(String file0, String mimeFile0) throws SMIMEException { 221 222 File virtualFile = new File(file0); 223 224 return getMimeTypeFromFileName(virtualFile, mimeFile0); 225 } 226 227 /*** 228 * Returns BASE64 encoded String with the break at the 76th character. Base64 229 * is necessary for encoding, for transport binary data by email in MIME format. After 230 * encoding, data must be split at the 76th charachter (maximum) by inserting 231 * CRLF characters. 232 * @param b0 input byte array 233 * @return BASE64 encoded String with the break at the 76th character 234 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 235 * it can be caused by non SMIMEException which can be one of the following: 236 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 237 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 238 * UnsupportedEncodingException. 239 */ 240 public static String getStringBASE64WithBreakOn76(byte[] b0) throws SMIMEException { 241 return getStringBASE64WithBreak(b0, 76); 242 } 243 244 /*** 245 * Returns BASE64 encoded byte array with the break at the 76th character. Base64 246 * is necessary for encoding, for transport binary data by email in MIME format. After 247 * encoding, data must be split at the 76th charachter (maximum) by inserting 248 * CRLF characters. 249 * @param b0 input byte array 250 * @return BASE64 encoded byte array with the break at the 76th character 251 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 252 * it can be caused by non SMIMEException which can be one of the following: 253 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 254 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 255 * UnsupportedEncodingException. 256 */ 257 public static byte[] getBASE64WithBreakOn76(byte[] b0) throws SMIMEException { 258 return getBASE64WithBreak(b0, 76); 259 } 260 261 /*** 262 * Returns BASE64 encoded String with the break at the 76th character. Base64 263 * is necessary for encoding, for transport binary data by email in MIME format. After 264 * encoding, data must be split at the 76th charachter (maximum) by inserting 265 * CRLF characters. 266 * @param s0 input String 267 * @return BASE64 encoded String with the break at the 76th character 268 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 269 * it can be caused by non SMIMEException which can be one of the following: 270 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 271 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 272 * UnsupportedEncodingException. 273 */ 274 public static String getStringBASE64WithBreakOn76(String s0) throws SMIMEException { 275 return getStringBASE64WithBreak(s0, 76); 276 } 277 278 /*** 279 * Returns BASE64 encoded byte array with the break at the 76th character. Base64 280 * is necessary for encoding, for transport binary data by email in MIME format. After 281 * encoding, data must be split at the 76th charachter (maximum) by inserting 282 * CRLF characters. 283 * @param s0 input String 284 * @return BASE64 encoded byte array with the break at the 76th character 285 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 286 * it can be caused by non SMIMEException which can be one of the following: 287 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 288 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 289 * UnsupportedEncodingException. 290 */ 291 public static byte[] getBASE64WithBreakOn76(String s0) throws SMIMEException { 292 return getBASE64WithBreak(s0, 76); 293 } 294 295 /*** 296 * Returns BASE64 encoded String with the break at the defined character. Base64 297 * is necessary for encoding, for transport binary data by email in MIME format. After 298 * encoding, data must be split at the 76th charachter (maximum) by inserting 299 * CRLF characters. 300 * @param b0 input byte array 301 * @param breakPosition0 position for breaking lines in resulted Base64 302 * encoding message content. It should be grather than 0 and less or equal 303 * than 76. 304 * @return BASE64 encoded String with the break at the 76th character 305 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 306 * it can be caused by non SMIMEException which can be one of the following: 307 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 308 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 309 * UnsupportedEncodingException. 310 */ 311 public static String getStringBASE64WithBreak(byte[] b0, int breakPosition0) throws SMIMEException { 312 if (breakPosition0 < 1 | breakPosition0 > 76) 313 throw new SMIMEException("org.webdocwf.util.smime.util.Base64ForMime", 1032); 314 StringBuffer b = new StringBuffer(64000); 315 316 try { 317 String a = new String(Base64.encode(b0), "ISO-8859-1"); 318 319 for (int i = 0; breakPosition0 * i < a.length(); i++) { 320 try { 321 b.append(a.substring(i * breakPosition0, (i + 1) * breakPosition0)); 322 } catch (IndexOutOfBoundsException e) { 323 b.append(a.substring(i * breakPosition0)); 324 break; 325 } 326 b.append("\r\n"); 327 } 328 } catch (Exception e) { 329 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 330 e, "getStringBASE64WithBreak"); 331 } 332 return b.toString(); 333 } 334 335 /*** 336 * Returns BASE64 encoded byte array with the break at the defined character. Base64 337 * is necessary for encoding, for transport binary data by email in MIME format. After 338 * encoding, data must be split at the 76th charachter (maximum) by inserting 339 * CRLF characters. 340 * @param b0 input byte array 341 * @param breakPosition0 position for breaking lines in resulted Base64 342 * encoding message content. It should be grather than 0 and less or equal 343 * than 76. 344 * @return BASE64 encoded byte array with the break at the 76th character 345 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 346 * it can be caused by non SMIMEException which can be one of the following: 347 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 348 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 349 * UnsupportedEncodingException. 350 */ 351 public static byte[] getBASE64WithBreak(byte[] b0, int breakPosition0) throws SMIMEException { 352 byte[] finalBASE64 = null; 353 354 try { 355 finalBASE64 = getStringBASE64WithBreak(b0, breakPosition0).getBytes("ISO-8859-1"); 356 } catch (Exception e) { 357 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 358 e, "getBASE64WithBreak"); 359 } 360 return finalBASE64; 361 } 362 363 /*** 364 * Returns BASE64 encoded String with the break at the defined character. Base64 365 * is necessary for encoding, for transport binary data by email in MIME format. After 366 * encoding, data must be split at the 76th charachter (maximum) by inserting 367 * CRLF characters. 368 * @param s0 input String 369 * @param breakPosition0 position for breaking lines in resulted Base64 370 * encoding message content. It should be grather than 0 and less or equal 371 * than 76. 372 * @return BASE64 encoded String with the break at the 76th character 373 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 374 * it can be caused by non SMIMEException which can be one of the following: 375 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 376 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 377 * UnsupportedEncodingException. 378 */ 379 public static String getStringBASE64WithBreak(String s0, int breakPosition0) throws SMIMEException { 380 byte[] bArray; 381 382 try { 383 bArray = s0.getBytes("ISO-8859-1"); 384 } catch (Exception e) { 385 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 386 e, "getStringBASE64WithBreak"); 387 } 388 return getStringBASE64WithBreak(bArray, breakPosition0); 389 } 390 391 /*** 392 * Returns BASE64 encoded byte array with the break at the defined character. 393 * Base64 is necessary for encoding, for transport binary data by email in MIME 394 * format. After encoding, data must be split at the 76th charachter (maximum) 395 * by inserting CRLF characters. 396 * @param s0 input String 397 * @param breakPosition0 position for breaking lines in resulted Base64 398 * encoding message content. It should be grather than 0 and less or equal 399 * than 76. 400 * @return BASE64 encoded byte array with the break at the 76th character 401 * @exception SMIMEException in case of wrong parameter breakPosition0. Also, 402 * it can be caused by non SMIMEException which can be one of the following: 403 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 404 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException or 405 * UnsupportedEncodingException. 406 */ 407 public static byte[] getBASE64WithBreak(String s0, int breakPosition0) throws SMIMEException { 408 byte[] bArray; 409 410 try { 411 bArray = s0.getBytes("ISO-8859-1"); 412 } catch (Exception e) { 413 throw SMIMEException.getInstance("org.webdocwf.util.smime.util.MimeAssist", 414 e, "getBASE64WithBreak"); 415 } 416 return getBASE64WithBreak(bArray, breakPosition0); 417 } 418 419 }

This page was automatically generated by Maven